package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Zbxx;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class ZbxxForm extends BasePage implements Serializable {
    private GenericManager<Zbxx, Long> zbxxManager;
    private Zbxx zbxx = new Zbxx();
    private Long id;
    private Jbqk jbqk;
    public void setJbqk(Jbqk jbqk)
	{
		this.jbqk=jbqk;
	}
    public void setZbxxManager(GenericManager<Zbxx, Long> zbxxManager) {
        this.zbxxManager = zbxxManager;
    }

    public Zbxx getZbxx() {
        return zbxx;
    }

    public void setZbxx(Zbxx zbxx) {
        this.zbxx = zbxx;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        zbxxManager.remove(zbxx.getId());
        addMessage("zbxx.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            zbxx = zbxxManager.get(id);
        } else {
            zbxx = new Zbxx();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (zbxx.getId() == null || zbxx.getId() == 0);
        
        zbxx.setJbqk(jbqk);
        zbxx.setUsername(getRequest().getRemoteUser());
        zbxx.setOptdate(new Date());
        zbxx.setOperation(isNew?Constants.OPERATION_ADD:Constants.OPERATION_UPDATE);
        
        zbxxManager.save(zbxx);

        String key = (isNew) ? "zbxx.added" : "zbxx.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
          	
     	   this.id=this.zbxx.getId();
              return edit();

        }
    }
} 